package com.mashibing.servicedriveruser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.servicedriveruser.remote.ServiceMapClient;
import org.mashibing.dto.Car;
import com.mashibing.servicedriveruser.mapper.CarMapper;
import com.mashibing.servicedriveruser.service.CarService;
import org.mashibing.dto.ResponseResult;
import org.mashibing.response.TerminalResponse;
import org.mashibing.response.TrackResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:
 * @create: 2024/6/27 15:29
 * @description:
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    private ServiceMapClient serviceMapClient;


    @Override
    public ResponseResult addCar(Car car) {

        this.baseMapper.insert(car);

        // 获得此车辆的终端id：tid
        ResponseResult<TerminalResponse> responseResult = serviceMapClient.addTerminal(car.getVehicleNo(), car.getId()+"");
        String tid = responseResult.getData().getTid();
        car.setTid(tid);

        // 获得此车辆的轨迹id：trid
        ResponseResult<TrackResponse> trackResponseResponseResult = serviceMapClient.addTrack(tid);
        String trid = trackResponseResponseResult.getData().getTrid();
        String trname = trackResponseResponseResult.getData().getTrname();
        car.setTrid(trid);
        car.setTrname(trname);

        this.baseMapper.updateById(car);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<Car> getCarById(Long id) {
        Car cars = this.baseMapper.selectOne(new QueryWrapper<Car>().eq("id",id));
        return ResponseResult.success(cars);
    }
}
